package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.QuitClassDao;
import com.example.domain.QuitClass;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.QuitClassService;
import com.example.tool.DateTool;

@Service
public class QuitClassServiceImpl implements QuitClassService {

    @Autowired
    QuitClassDao quitClassDao;

    /**
     * @describe 添加退班信息
     */
    @Override
    public Result addQuitClass(QuitClass quitClass) {
        if (quitClass != null) {
            // 获取系统当前时间作为记录创建时间
            quitClass.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        QuitClass quitClass1 = quitClassDao.save(quitClass);

        if (quitClass1 != null) {
            return ResultFactory.buildSuccessResult(quitClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新退班信息
     */
    @Override
    public Result updateQuitClass(QuitClass quitClass) {
        if (quitClass != null) {
            // 获取系统当前时间作为记录更新时间
            quitClass.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        QuitClass quitClass1 = quitClassDao.saveAndFlush(quitClass);

        if (quitClass1 != null) {
            return ResultFactory.buildSuccessResult(quitClass1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除退班信息
     */
    @Override
    public Result deleteQuitClassById(String quitId) {
        int row = quitClassDao.deleteByQuitId(Integer.parseInt(quitId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查詢所有退班信息
     */
    @Override
    public Result queryAllQuitClass() {
        List<QuitClass> list = quitClassDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询退班信息
     */
    @Override
    public Result queryQuitClassById(String quitId) {
        List<QuitClass> list = quitClassDao.findByQuitId(Integer.parseInt(quitId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加退班信息
     */
    @Override
    public Result addQuitClassByExcel(List<QuitClass> list) {
        List<QuitClass> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = quitClassDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据退班编号批量删除退班信息
     */
    @Override
    public Result deleteQuitClassByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = quitClassDao.deleteByQuitIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 退班信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("originalClass")) {

            list = quitClassDao.findByOriginalClassLike(info[1]);

        } else if (info[0].equals("quitStatus")) {

            list = quitClassDao.findByQuitStatusLike(info[1]);

        } else if (info[0].equals("quitReason")) {

            list = quitClassDao.findByQuitReasonLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = quitClassDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = quitClassDao.findByNickNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有退班和用户信息
     */
    @Override
    public Result queryAllQuitClassAndUsersInfo() {
        // TODO 查询所有退班和用户信息
        List<Map<String, Object>> list = quitClassDao.queryAllQuitClassAndUsersInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectQuitClassByUserId(Integer userId) {
        // TODO 根据学生ID查询学生退班信息
        List<Map<String, Object>> list = quitClassDao.selectQuitClassByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
